# 饼图 男女 必修课绩点 区分

import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Grid, Liquid, Page, Pie, Timeline
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Faker
from pyecharts.globals import ThemeType

from Page.sql import search_average_compulsory_by_sex

df = pd.DataFrame(search_average_compulsory_by_sex(), columns=['性别', '平均绩点'])  # 这里列出你想要的列名

df = df.drop(df[df['平均绩点'] == 0].index)  # 清洗没有数据的人
list_girl = []
list_boy = []
# 筛选平均绩点在0.0到1.0之间的每个性别的人数
count_0_to_1 = df[(df['平均绩点'] < 1.0)].groupby('性别').size()
list_girl.append(["绩点 < 1.0", int(count_0_to_1["女"])])
list_boy.append(["绩点 < 1.0", int(count_0_to_1["男"])])
# 筛选平均绩点在1.0到2.0之间的每个性别的人数
count_1_to_2 = df[(df['平均绩点'] >= 1.0) & (df['平均绩点'] < 2.0)].groupby('性别').size()
list_girl.append(["绩点 1.0~2.0", int(count_1_to_2["女"])])
list_boy.append(["绩点 1.0~2.0", int(count_1_to_2["男"])])
# 筛选平均绩点在2.0到3.0之间的每个性别的人数
count_2_to_3 = df[(df['平均绩点'] >= 2.0) & (df['平均绩点'] < 3.0)].groupby('性别').size()
list_girl.append(["绩点 2.0~3.0", int(count_2_to_3["女"])])
list_boy.append(["绩点 2.0~3.0", int(count_2_to_3["男"])])
# 筛选平均绩点在3.0到4.0之间的每个性别的人数
count_3_to_4 = df[(df['平均绩点'] >= 3.0) & (df['平均绩点'] < 4.0)].groupby('性别').size()
list_girl.append(["绩点 3.0~4.0", int(count_3_to_4["女"])])
list_boy.append(["绩点 3.0~4.0", int(count_3_to_4["男"])])
# 筛选平均绩点在4.0到5.0之间的每个性别的人数
count_4_to_5 = df[(df['平均绩点'] >= 4.0)].groupby('性别').size()
list_girl.append(["绩点 > 4.0", int(count_4_to_5["女"])])
list_boy.append(["绩点 > 4.0", int(count_4_to_5["男"])])
list_total = [list_girl, list_boy]
print(list_girl)

# # grid.render("水球图.html")
list_name = ["女生", "男生"]

tl = Timeline()
for i in range(0, 2):
    pie = (
        Pie()
        .add(
            "",
            list_total[i],
            radius=["40%", "75%"],
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="2020级计算机学院%s绩点分布图" % list_name[i]),
            legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),
        )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )
    tl.add(pie, list_name[i])
    tl.add_schema(is_auto_play=True)
tl.render("饼图.html")
